package com.XinYun.Library.modules.Login.Service.Impl;

import com.XinYun.Library.modules.Login.Entity.User;
import com.XinYun.Library.modules.Login.Service.UserService;
import com.XinYun.Library.utils.FormatUtil.FormatUtil;
import com.XinYun.Library.utils.SqlUtils.DruidUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserServiceImpl implements UserService {
    /**
     * 根据邮箱找到用户信息，从而判断其学号与邮箱是否匹配
     * @param sid
     * @return
     * @throws SQLException
     */
    @Override
    public String findUserBySid(String sid) throws SQLException {
        String mail = null;
        Connection connection = DruidUtil.getConnection();
        String sql = "select mail from register where sid = ?;";
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setString(1,sid);
        ResultSet rs = ps.executeQuery();
        if (rs.next()){
            mail = rs.getString("mail");
        }
        DruidUtil.close(rs,ps,connection);
        return mail;
    }

    /**
     * 判断数据库中用户的密码是否更改成功
     * @param password
     * @param sid
     * @return
     * @throws SQLException
     */
    @Override
    public boolean updatePwd(String password,String sid) throws SQLException {
        Connection connection = DruidUtil.getConnection();
        String sql = "update register set password = ? where sid = ?;";
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setString(1,password);
        ps.setString(2,sid);
        int count = ps.executeUpdate();
        if (count != 0) {
            return true;
        }
        DruidUtil.close(null,ps,connection);
        return false;
    }

}
